﻿using StackExchange.Redis;

namespace Apex.ASPNETCore.Cache
{
    public class RedisHelper : IRedisHelper
    {
        private readonly IDatabase _db;

        public RedisHelper(IConnectionMultiplexer redis)
        {
            _db = redis.GetDatabase();
        }

        // 字符串操作
        public async Task<bool> SetStringAsync(string key, string value, TimeSpan? expiry = null)
        {
            return await _db.StringSetAsync(key, value, expiry);
        }
        public async Task<string> GetStringAsync(string key)
        {
            return await _db.StringGetAsync(key);
        }
        public async Task<bool> DeleteStringAsync(string key)
        {
            return await _db.KeyDeleteAsync(key);
        }

        // 哈希操作
        public async Task<bool> HashSetAsync(string key, string field, string value)
        {
            return await _db.HashSetAsync(key, field, value);
        }
        public async Task<string> HashGetAsync(string key, string field)
        {
            return await _db.HashGetAsync(key, field);
        }
        public async Task<long> HashDeleteAsync(string key, string[] fields)
        {
            RedisValue[] redisValues = Array.ConvertAll(fields, field => (RedisValue)field);
            return await _db.HashDeleteAsync(key, redisValues);
        }

        // 列表操作
        public async Task<long> ListLeftPushAsync(string key, string value)
        {
            return await _db.ListLeftPushAsync(key, value);
        }
        public async Task<long> ListRightPushAsync(string key, string value)
        {
            return await _db.ListRightPushAsync(key, value);
        }
        public async Task<string> ListLeftPopAsync(string key)
        {
            return await _db.ListLeftPopAsync(key);
        }
        public async Task<string> ListRightPopAsync(string key)
        {
            return await _db.ListRightPopAsync(key);
        }

        // 集合操作
        public async Task<bool> SetAddAsync(string key, string value)
        {
            return await _db.SetAddAsync(key, value);
        }
        public async Task<bool> SetRemoveAsync(string key, string value)
        {
            return await _db.SetRemoveAsync(key, value);
        }
        public async Task<bool> SetContainsAsync(string key, string value)
        {
            return await _db.SetContainsAsync(key, value);
        }
        public async Task<string[]> SetMembersAsync(string key)
        {
            var members = await _db.SetMembersAsync(key);
            return Array.ConvertAll(members, member => (string)member);
        }

        // 有序集合操作
        public async Task<bool> SortedSetAddAsync(string key, string member, double score)
        {
            return await _db.SortedSetAddAsync(key, member, score);
        }
        public async Task<bool> SortedSetRemoveAsync(string key, string member)
        {
            return await _db.SortedSetRemoveAsync(key, member);
        }
        public async Task<double?> SortedSetScoreAsync(string key, string member)
        {
            return await _db.SortedSetScoreAsync(key, member);
        }
        public async Task<long?> SortedSetRankAsync(string key, string member)
        {
            return await _db.SortedSetRankAsync(key, member);
        }

        // 键操作
        public async Task<bool> KeyExistsAsync(string key)
        {
            return await _db.KeyExistsAsync(key);
        }
        public async Task<long> KeyExpireAsync(string key, TimeSpan expiry)
        {
            return await _db.KeyExpireAsync(key, expiry) ? 1 : 0;
        }
        public async Task<long> KeyDeleteAsync(string key)
        {
            return await _db.KeyDeleteAsync(key) ? 1 : 0;
        }
    }
}